/**
 * 避免全局命名空间污染
   总是将代码包裹在一个
   立即的函数表达式里面，形成独立模块
 */
/*;(function($,window,document,undefined) {
    'use strict'; // 必须放到最前面一行
	var USE_STREAM_UPLOAD = true;
	var CLIENT_ID = "8711ca42bf58b526";
	var ACCESS_TOKEN = "d749887da3311f48fc6656805cb626ac";
	var REFRESH_TOKEN = "7a1f1bb809ff4a0fccff1aa82fb13b26";
	// 刷新token地址 
	var ACCESS_TOKEN_FRESH_URL = "https://api.youku.com/oauth2/token.json";
	jQuery(document).ready(function() {
	    var param = {client_id: CLIENT_ID,access_token:ACCESS_TOKEN,completeCallback:"uploadComplete"};
	    youkuUploadInit(param);
	});
	//上传完成时回调方法
	function uploadComplete(data) {
		alert(data);
        alert("videoid="+data.videoid+";title="+data.title);
		alert(data);
        alert("videoid="+data.videoid+";title="+data.title);
        uploadagain();
	}
}(jQuery,window,document));*/
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'busi/video/list',
        datatype: "json",
        colModel: [			
			{ label: 'id_', name: 'id_', width: 45, key: true,hidden:true},
			{ label: 'id', name: 'id', width: 45, key: true,hidden:true},
			{ label: 'videoId', name: 'videoId', width: 45, hidden:true},
            { label: '标题', name: 'title', width: 75},
            { label: '视频链接', name: 'link', width: 75},
			{ label: '视频截图', name: 'thumbnail', width: 75},
			{ label: '视频截图 (大)', name: 'bigThumbnail', width: 75},
			{ label: '时长(秒)', name: 'duration', width: 75 },
			{ label: '优酷分类', name: 'category', width: 75 },
			{ label: '自定义分类', name: 'sort', width: 75 },
			{ label: '状态', name: 'state', width: 55 },
			{ label: '标签', name: 'tags', width: 55 },
			{ label: '总播放次数', name: 'view_count', width: 35},
			{ label: '发布时间', name: 'published', width: 45},
			{ label: '视频格式', name: 'streamtypes', width: 45,hidden:true},
			{ label: '部门', name: 'deptId', width: 45,hidden:true},
			{ label: '科室', name: 'deptNO', width: 45,hidden:true},
			{ label: '科室名称', name: 'deptNOName', width: 45},
			{ label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
				return value === 0 ? 
						'<span class="label label-danger">禁用</span>' : 
						'<span class="label label-success">正常</span>';
			}},
			{ label: '主讲人', name: 'speaker', width: 120}
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    getSelectOpt("deviceType","DEVICE_TYPE",true);
});



var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			title: null
		},
		showList: true,
		title:null,
		video:{ 
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.video = {title:null, producer:null, typeNo:null,facadeSize:null};
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
			vm.getPro(id);
		},
		del: function () {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "busi/video/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.video.id_ == null ? "busi/video/save" : "busi/video/update";
			var arr =$("#deviceType").multipleSelect("getSelects");
			if(arr.length===0) {
				vm.video.deviceType="";
			} else {
				vm.video.deviceType=arr[0];
			}
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.video),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getPro: function(proId){
			$.get(baseURL + "busi/video/info/"+proId, function(r){
				vm.video = r.video;
				var arrs=[vm.video.deviceType];
				$("#deviceType").multipleSelect("setSelects",arrs);
			});
		},
	 
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'title': vm.q.title},
                page:page
            }).trigger("reloadGrid");
		}
	}
});